package com.huawei.hwmdemo.function;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import com.huawei.cloudlink.openapi.HWMSdk;
import com.huawei.cloudlink.openapi.api.param.JoinConfParam;
import com.huawei.cloudlink.openapi.api.param.LoginAuthType;
import com.huawei.cloudlink.openapi.api.param.LoginParam;
import com.huawei.cloudlink.openapi.api.param.PasswordJoinConfParam;
import com.huawei.hwmbiz.eventbus.LoginResult;
import com.huawei.hwmconf.presentation.error.ErrorMessageFactory;
import com.huawei.hwmconf.sdk.util.Utils;
import com.huawei.hwmdemo.R;
import com.huawei.hwmfoundation.HwmContext;
import com.huawei.hwmfoundation.callback.HwmCallback;
import com.huawei.hwmdemo.utils.DemoUtil;
import com.huawei.hwmfoundation.callback.HwmCancelableCallBack;

public class LoginAndJoinSubThread implements Runnable{

    private Application application;
    private String nickName;

    // 账号密码
//    String account = "18476925171";
//    String password = "2262093abc";
    private String account;
    private String password;

    // 会议id和密码
//    String mId = "938511663";
//    String mPassword = "993350";
    private String mId;
    private String mPassword;

    public LoginAndJoinSubThread(Application application, String nickName, String account, String password, String mId, String mPassword) {
        this.application = application;
        this.nickName = nickName;
        this.account = account;
        this.password = password;
        this.mId = mId;
        this.mPassword = mPassword;
    }

    private void login() {

        LoginParam loginParam = new LoginParam()
                .setLoginAuthType(LoginAuthType.Account_And_Password)       //登录方式的类别
                .setAccount(account)        //登录帐号
                .setPassword(password);      //登录密码

        HWMSdk.getOpenApi(application).login(loginParam, new HwmCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                if (loginResult != null) {
                    Log.e("LoginSubThread", "登录成功");
                    Log.e("LoginSubThread", loginResult.getUserUuid());
                } else {
                    Log.e("LoginSubThread", "登录成功");
                }

                joinConf();
            }

            @Override
            public void onFailed(int retCode, String desc) {
                Log.e("LoginSubThread", String.valueOf(retCode));
                Log.e("LoginSubThread", "登录错误：" + DemoUtil.getErrMsg(application.getApplicationContext(), retCode));
            }
        });
    }

    private void joinConf() {

        if (TextUtils.isEmpty(mId)) {
            DemoUtil.showToast("会议ID不能为空");
            return;
        }


        JoinConfParam joinConfParam;
        joinConfParam = new PasswordJoinConfParam()
                .setPassword(mPassword)
                .setConfId(mId)
                .setNickname(nickName)
                .setCameraOn(true)
                .setMicOn(true);

        HWMSdk.getOpenApi(application).joinConf(joinConfParam, new HwmCancelableCallBack<Void>() {
            @Override
            public void onSuccess(Void ret) {
                Log.e("LoginSubThread", "入会成功");
            }

            @Override
            public void onFailed(int retCode, String desc) {
                String err = ErrorMessageFactory.create(Utils.getApp(), retCode);
                if (TextUtils.isEmpty(err)) {
                    err = Utils.getResContext().getString(R.string.hwmconf_join_fail_tip);
                }
                DemoUtil.showToast("加入会议失败: " + err);
                Log.e("LoginSubThread", "入会失败");
            }

            @Override
            public void onCancel() {
                Log.e("LoginSubThread", "取消");
            }
        });
    }

    @Override
    public void run() {
        login();
    }
}
